package com.fangqiang.sprint_boot_web1.mybatisinterceptor;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Pattern;


@Component
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class MybatisIntercepts implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("========");
        Object[] args = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement)args[0];
        Object params = args[1];


        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        // 获取替换值之前的sql
        String sql = mappedStatement.getBoundSql(params).getSql();

        if(sqlCommandType == SqlCommandType.DELETE){
            return invocation.proceed();
        }

        //执行后面业务逻辑
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        System.out.println("init");
    }
}